﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSV.Models;
using TSV.Library.Exceptions;
using TSV.Library.Attributes;
using TSV.Library.Common;
using System.Configuration;
using System.Web.Routing;
using TSV.Areas.Administration.Models;
namespace TSV.Controllers
{
    public abstract class BaseViewController : Controller
    {
        private string msgHoder = "<div id=\"systemmessage\" class=\"{0}\">{1}</div>";
        private CategoryDao cateDao = new CategoryDao();
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            int selected_Province = 0;
            int selected_District = 0;
            int selected_Category = 0;
            string navigation = "";
            string separate = " » ";
            CategoryDao catDao = new CategoryDao();


            if (requestContext.HttpContext.Session[SessionKey.GLOBALSEARCHDATA] != null)
            {

                Hashtable hash = (Hashtable)Session[SessionKey.GLOBALSEARCHDATA];

                string value = (string)hash[Constants.GLOBAL_FILTER_CATEGORY];
                if (!string.IsNullOrEmpty(value) && CheckUtil.IsPositiveNumber(value))
                {
                    selected_Category = int.Parse(value);
                }
                string province_value = (string)hash[Constants.GLOBAL_FILTER_PROVINCE];
                if (!string.IsNullOrEmpty(province_value) && CheckUtil.IsPositiveNumber(province_value))
                {
                    selected_Province = int.Parse(province_value);

                }
                string district_value = (string)hash[Constants.GLOBAL_FILTER_DISTRICT];
                if (!string.IsNullOrEmpty(district_value) && CheckUtil.IsPositiveNumber(district_value))
                {
                    selected_District = int.Parse(district_value);

                }
            }
            if (selected_Category > 0)
            {
                Category objCategory = catDao.GetByID(selected_Category);
                navigation = separate + objCategory.CategoryName;
            }
            ViewData["MasterProvinceList"] = new SelectList(new CategoryDao().GetListProvince(), "ID", "ProvinceName", selected_Province);
            if (selected_Province > 0)
            {
                Province objPronvince = catDao.GetProvinceByID(selected_Province);
                navigation += separate + objPronvince.ProvinceName;
                ViewData["MasterDistrictList"] = new SelectList(catDao.GetDitrictListByProvinceID(selected_Province), "ID", "DistrictName", selected_District);
            }
            else
            {
                ViewData["MasterDistrictList"] = new SelectList(new List<string>(), "ID", "DistrictName", selected_District);
            }
            if (selected_District > 0)
            {
                District objDsitrict = catDao.GetDistrictByID(selected_District);
                navigation += separate + objDsitrict.DistrictName;
            }

            //Load All Categories List
            List<Category> parentList = cateDao.GetParentList();
            List<Category> categoryList = new List<Category>();
            //append child list
            foreach (Category parent in parentList)
            {
                categoryList.Add(parent);
                List<Category> childList = cateDao.GetChildListByParentId(parent.ID);
                foreach (Category child in childList)
                {
                    child.CategoryName = parent.CategoryName + " --> " + child.CategoryName;
                    categoryList.Add(child);
                }
            }

            ViewData["GlobalCategory"] = new SelectList(categoryList, "ID", "CategoryName", selected_Category);
            if (!string.IsNullOrEmpty(navigation))
            {
                navigation = navigation.Substring(separate.Length - 1, navigation.Length - separate.Length + 1);
            }
            ViewData["NavigationLink"] = navigation;
        }
        public BaseViewController()
        {

            //Set value for combobox of search
            //int selected_Province = 0;
            //int selected_District = 0;
            //int selected_Category = 0;
            //if (Session["GlobalSearchData"] != null)
            //{
            //    Hashtable hash = (Hashtable)Session[SessionKey.GLOBALSEARCHDATA];

            //    string value = (string)hash[Constants.GLOBAL_FILTER_CATEGORY];
            //    if (!string.IsNullOrEmpty(value) && CheckUtil.IsPositiveNumber(value))
            //    {
            //        selected_Category = int.Parse(value);
            //    }
            //    string province_value = (string)hash[Constants.GLOBAL_FILTER_PROVINCE];
            //    if (!string.IsNullOrEmpty(province_value) && CheckUtil.IsPositiveNumber(province_value))
            //    {
            //        selected_Province = int.Parse(province_value);
            //    }
            //    string district_value = (string)hash[Constants.GLOBAL_FILTER_DISTRICT];
            //    if (!string.IsNullOrEmpty(district_value) && CheckUtil.IsPositiveNumber(district_value))
            //    {
            //        selected_District = int.Parse(district_value);
            //    }
            //}
            //ViewData["MasterProvinceList"] = new SelectList(new CategoryDao().GetListProvince(), "ID", "ProvinceName", selected_Province);
            //ViewData["MasterDistrictList"] = new SelectList(new List<string>(), "ID", "DistrictName", selected_District);
            //ViewData["MasterSubCategory"] = new SelectList(new CategoryDao().GetList(), "ID", "CategoryName", selected_Category);
            //

            //ViewData["MasterPromotion"] = new PromotionDao().GetList();
            ViewData["MasterCategory"] = new CategoryDao().GetList();

            //ViewData["NewPartner"] = new PartnerDao().GetListNewPartner();
            ViewData["FooterMenu"] = new CategoryDao().GetList();
            //
            ViewData["MasterBanner"] = new AdvertisingDao().GetListBigPartner();
            //
            ViewData["MasterBottom"] = new AdvertisingDao().GetAdvertisingBottom();

            ViewData["AdvertisingLeft"] = new AdvertisingDao().GetAdvertisingLeft();
        }


        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                string message = "";

                Exception ex = filterContext.Exception;
                filterContext.ExceptionHandled = true;

                if (ex is CrmException)
                {
                    CrmException ge = (CrmException)ex;
                    if (ex is ForbiddenExceptionOnCurrentPage)
                    {
                        string controllerName = filterContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
                        Message msg = new Message(MessageConstants.E0002, MessageType.Error);
                        ShowMessage(msg);
                        this.RedirectToAction("Index", controllerName).ExecuteResult(this.ControllerContext);
                    }
                    else
                    {
                        message = ge.ErrorMessage;
                        ViewData["ErrorMessage"] = message;
                        this.View("Error").ExecuteResult(this.ControllerContext);
                    }
                }
                else if (ex is Exception)
                {
                    message = ex.Message;
                    ViewData["ErrorMessage"] = message;
                    this.View("Error").ExecuteResult(this.ControllerContext);
                }
            }
        }
        #region helper methods


        //duy hung sua
        protected void ShowMessage(Message msg)
        {
            string message = string.Empty;
            if (msg.MsgType == MessageType.Error)
            {
                message = string.Format(msgHoder, "msgError", msg.MsgText);
            }
            else if (msg.MsgType == MessageType.Warning)
            {
                message = string.Format(msgHoder, "msgWarning", msg.MsgText);
            }
            else
            {
                message = string.Format(msgHoder, "msgSuccess", msg.MsgText);
            }

            TempData["Message"] = message;
        }

        //duy hung sua
        protected void ShowAlertMessage(Message msg)
        {
            TempData["Message"] = "<script>alert('" + HttpUtility.HtmlDecode(msg.MsgText) + "');</script>";
        }
        //Tuan them
        protected void ShowAlertPopup(Message msg)
        {
            TempData["Message"] = "<script>$(document).ready(function () { JS_LIB.msgBox('" + HttpUtility.HtmlDecode(msg.MsgText) + "', 300);});</script>";
        }
        #endregion

        public class FileUploadJsonResult : JsonResult
        {
            public override void ExecuteResult(ControllerContext context)
            {
                this.ContentType = "text/html";
                context.HttpContext.Response.Write("<textarea>");
                base.ExecuteResult(context);
                context.HttpContext.Response.Write("</textarea>");
            }
        }

    }
}
